<%@page import="com.mbbmap.util.EncryptionHelper"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import = "org.json.simple.*, java.util.TreeMap, com.classes.Casemast, com.classes.CaseOutlet, java.util.ArrayList, java.net.URLEncoder, org.owasp.encoder.Encode" %>
<%@ page import = "com.mbbmap.util.Constants" %>
<%@ page import = "com.mbbmap.security.dao.*" %>
<%@ include file="/jsp/map/include/user-dao.jsp" %>	
<%@ page import = "java.net.URLEncoder" %>
<%
int merchant_id = 0;
int case_id = 0;

String str_merchant_id = "";
String str_case_id = "";

String serial_no = "";
String app_no = "";
int outlet_id = 0;
String trading_name = "";
String reg_name = "";
String merchant_pic = "";
String merchant_handphone = "";

// address fields
String address = "";
String line_1 = "";
String line_2 = "";
String line_3 = "";
String line_4 = "";
String postal_code = "";
String city = "";
String region_code = "002";

String inst_status = "";
String reschedule_date = "";
JSONObject terminal_detail = new JSONObject();

JSONObject vendor_arr = null;
JSONObject vendor_details = null;

ArrayList<TreeMap<String, String>> tid_facilities = new ArrayList<TreeMap<String, String>>();
JSONObject facility_tids = null;

if(request.getAttribute("serial_no") != null) {
	serial_no = request.getAttribute("serial_no").toString();
}

if(request.getAttribute("m_case") != null){
	Casemast m_case = (Casemast) request.getAttribute("m_case");
	
	case_id = (int) m_case.getId();
	merchant_id = (int) m_case.getMerchant().getId();
	
	str_merchant_id =EncryptionHelper.encrypt(String.valueOf(m_case.getMerchant().getId()));
	str_case_id =EncryptionHelper.encrypt(String.valueOf(m_case.getId()));
	
	app_no = m_case.getReferenceNo();
	reg_name = m_case.getMerchant().getRegisteredName();
}

if(request.getAttribute("c_outlet") != null) {
	CaseOutlet c_outlet = (CaseOutlet) request.getAttribute("c_outlet");
	
	outlet_id = (int) c_outlet.getOutlet().getId();
	trading_name = c_outlet.getOutlet().getName();
	
	if(c_outlet.getOutlet().getAddress().length() > 0) {
		JSONObject obj = (JSONObject) JSONValue.parse(c_outlet.getOutlet().getAddress());

		line_1 = (obj.get("line_1") == null) ? "" : obj.get("line_1").toString().trim();
		line_2 = (obj.get("line_2") == null) ? "" : obj.get("line_2").toString().trim();
		line_3 = (obj.get("line_3") == null) ? "" : obj.get("line_3").toString().trim();
		line_4 = (obj.get("line_4") == null) ? "" : obj.get("line_4").toString().trim();
		postal_code = (obj.get("postal_code") == null) ? "" : obj.get("postal_code").toString().trim();
		city = (obj.get("city") == null) ? "" : obj.get("city").toString().trim();
		region_code = (obj.get("region_code") == null) ? "" : obj.get("region_code").toString().trim();
		
		if(region_code.equals("002")) {
			region_code = "002 - Sabah";
		} else if(region_code.equals("003")) {
			region_code = "003 - Sarawak";
		} else if(region_code.equals("004")) {
			region_code = "004 - Perlis";
		} else if(region_code.equals("005")) {
			region_code = "005 - Kedah";
		} else if(region_code.equals("006")) {
			region_code = "006 - Pulau Pinang";
		} else if(region_code.equals("007")) {
			region_code = "007 - Perak";
		} else if(region_code.equals("008")) {
			region_code = "008 - Kelantan";
		} else if(region_code.equals("009")) {
			region_code = "009 - Terengganu";
		} else if(region_code.equals("010")) {
			region_code = "010 - Pahang";
		} else if(region_code.equals("011")) {
			region_code = "011 - Selangor";
		} else if(region_code.equals("012")) {
			region_code = "012 - Negeri Sembilan";
		} else if(region_code.equals("013")) {
			region_code = "013 - Melaka";
		} else if(region_code.equals("014")) {
			region_code = "014 - Johor";
		} else if(region_code.equals("015")) {
			region_code = "015 - Wilayah Persekutuan";
		}
		
		if(line_1.length() > 0) { address += line_1 + "\n"; }
		if(line_2.length() > 0) { address += line_2 + "\n"; }
		if(line_3.length() > 0) { address += line_3 + "\n"; }
		if(line_4.length() > 0) { address += line_4 + "\n"; }
		if(postal_code.length() > 0) { address += postal_code + "\n"; }
		if(city.length() > 0) { address += city + "\n"; }
		if(region_code.length() > 0) { address += region_code; }
	}
	
	if(c_outlet.getPosForm().length() > 0) {
		JSONObject obj = (JSONObject) JSONValue.parse(c_outlet.getPosForm());
		
		merchant_pic = (obj.get("marchant_pic") == null) ? "" : obj.get("marchant_pic").toString();
		merchant_handphone = (obj.get("merchant_handphone") == null) ? "" : obj.get("merchant_handphone").toString();
		
		vendor_arr = (JSONObject) obj.get("vendor_arr");
		
		if(vendor_arr != null){
			if(request.getAttribute("vendor_name") != null){
				vendor_details = (JSONObject) vendor_arr.get(request.getAttribute("vendor_name"));
			}
			
			if(vendor_details != null){			
				facility_tids = (vendor_details.get("facility_tids") == null ? null : (JSONObject) vendor_details.get("facility_tids"));
			}
		}
	}
	
	
	if(c_outlet.getPosInstallation().length() > 0){
		JSONObject obj = (JSONObject) JSONValue.parse(c_outlet.getPosInstallation());
		
		if(request.getAttribute("vendor_name") != null){
			JSONObject vendor_obj = (JSONObject) obj.get(request.getAttribute("vendor_name"));
			
			if(vendor_obj != null) {
				terminal_detail = (JSONObject) vendor_obj.get("terminal_detail");
				inst_status = vendor_obj.get("inst_status").toString();
				reschedule_date = vendor_obj.get("reschedule_date").toString();
			}
		}
	}
	
}

if(facility_tids != null) {
	String[] arr_type = new String[] {"Wired", "Wireless", "MPOS"};
	
	for(Object s : facility_tids.keySet()){
		JSONObject tid_type_obj = (JSONObject) facility_tids.get(s);
		
		for(String t  : arr_type) {
			if(tid_type_obj.get(t) != null) {
				JSONArray tid_obj = (JSONArray) tid_type_obj.get(t);
				
				for(int i=0; i<tid_obj.size(); i++){
					JSONObject tid_item = (JSONObject) tid_obj.get(i);
					
					TreeMap<String, String> item = new TreeMap<String, String>();
					item.put("facility", s.toString());
					item.put("mid_no", tid_item.get("mid_no").toString());
					item.put("mid_no2", tid_item.get("mid_no2").toString());
					item.put("tid_no", tid_item.get("tid_no").toString());
					
					if(terminal_detail.get(tid_item.get("tid_no")) != null){
						JSONObject terminal_item = (JSONObject) terminal_detail.get(tid_item.get("tid_no"));
						
						item.put("tid_serial", terminal_item.get("tid_serial").toString());
						item.put("sim_serial", terminal_item.get("sim_serial").toString());
						item.put("sam_serial", terminal_item.get("sam_serial").toString());
						item.put("install_date", terminal_item.get("install_date").toString());
					}
					else {
						item.put("tid_serial", "");
						item.put("sim_serial", "");
						item.put("sam_serial", "");
						item.put("install_date", "");
					}
					
					tid_facilities.add(item);
				}
			}
		}
	}
}


%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="/jsp/map/include/html-meta.jsp" />
<script src="<%= request.getContextPath() %>/js/merchant.js"></script>
<script src="<%= request.getContextPath() %>/js/statusaction.js"></script>
<script src="<%= request.getContextPath() %>/js/vendor.js"></script>

<title>MAYBANK</title>
</head>

<body>

<!-- Header -->
<%@ include file="/jsp/common/Header.jsp" %>	
<!-- End Header -->

<div class="row-fluid page">
	<div class="span12">

		<%
			/* String htmltitle = "<ul class='nav nav-tabs'>"
						+ 	"<li id='installation' class='active'><a href='javascript:tab.load(\"/NewApp.do?dispatch=vendor_installation\")'>Terminal-Vendor Installation Form</a></li>"
						+"</ul>"; */
						
			String htmltitle = "<ul class='nav nav-tabs'>"
						+ 	"<li id='pos'><a href='javascript:tab.load(\"/NewApp.do?dispatch=vendor_pos_3\")'>POS Form</a></li>"
						+ 	"<li id='installation' class='active'><a href='javascript:tab.load(\"/NewApp.do?dispatch=terminal_vendor_installation\")'>Terminal-Vendor Installation Form</a></li>"
						+"</ul>";						
						
			out.println(htmltitle);
		%>
		
		<form name="form">
		<input type="hidden" name="<%= Constants.TOKEN_KEY %>" value="<%= session.getAttribute(Action.TRANSACTION_TOKEN_KEY) %>" >
		<div class="form-horizontal">
			
			<div class="control-group">
				<label class="control-label">Serial No.</label>
				<div class="controls">
					<input id="serial_no" name="serial_no" type="text" value="<%= serial_no %>" />
					<button class="btn search" type="button" onclick="vendor.terminal_vendor_installation_search()">Search</button>
				</div>
			</div>
			
			<div class="control-group">
				<label class="control-label">Reference No.</label>
				<div class="controls">
					<input id="reference_id" name="reference_id" readonly="readonly" type="text" value="<%= app_no %>" />
				</div>
			</div>
			
			<div class="control-group">
				<label class="control-label">Trading Name</label>
				<div class="controls">
					<input id="trading_name" name="trading_name" readonly="readonly" type="text" value="<%= trading_name %>" />
					<input id="outlet_id" name="outlet_id" type="hidden" value="<%= EncryptionHelper.encrypt(outlet_id+"") %>" />
				</div>
			</div>
			
			<div class="control-group">
				<label class="control-label">Registered Name</label>
				<div class="controls">
					<input type="text" id="registered_name" readonly="readonly" value="<%= reg_name %>" />
				</div>
			</div>
			
			<div class="control-group">
				<label class="control-label">Address</label>
				<div class="controls">
					<textarea id="address" readonly="readonly" style="height:60px"><%= address %></textarea>
				</div>
			</div>
			
			
			<div class="row-fluid">
				<div class="span6">
					<div class="control-group">
						<label class="control-label">Merchant PIC</label>
						<div class="controls">
							<input type="text" id="marchant_pic" name="marchant_pic" readonly="readonly" value="<%= merchant_pic %>" />
						</div>
					</div>
				</div>
				
				<div class="span6">
					
					<div class="control-group">
						<label class="control-label">Handphone No.</label>
						<div class="controls">
							<input type="text" id="merchant_handphone" name="merchant_handphone" readonly="readonly" value="<%= merchant_handphone %>" />
						</div>
					</div>
				</div>
				
			</div>
			
			
			<div class="row-fluid">
				<%
					String vendor_name = "";
					if(vendor_details != null){
						vendor_name = vendor_details.get("name").toString();
					}
				%>
				<div id="vendor_install_detail" class="span10 detail" data-name="<%=vendor_name %>">
					
					<table class="tbl vendor_inst_tbl" style="width:100%">
						<tr>
							<td style="width:20%">Installation Status</td>
							<td style="width:80%">
								<select id="inst_status" name="inst_status">
									<option value=""></option>
									<% 
										String html = "<option value=\"Successful\">Successful</option>"
											+ "<option value=\"Cancel\">Cancel</option>"
											+ "<option value=\"Reschedule\">Reschedule</option>";
										
										html = html.replace("value=\"" + inst_status + "\"", "value=\"" + inst_status + "\" selected ");
										out.println(html);
									%>
								</select>
							</td>
						</tr>
						<tr>
							<td>Reschedule Date</td>
							<td><input type="text" id="reschedule_date" name="reschedule_date" placeholder="ddmmyyyy" value="<%= reschedule_date %>" /></td>
						</tr>
					</table>
					
				
				
				
					<div class="form-section">
						<div class="title">Terminal Detail</div>
						<table class="tbl vendor_pos_tbl_2 terminal_detail" style="width:90%; margin-bottom:20px"><thead>
							<tr>
								<th style="width:16%">POS MID</th>
								<th style="width:16%">AMEX MID</th>
								<th style="width:18%">Terminal ID</th>
								<th style="width:15%">Terminal Serial No.</th>
								<th style="width:15%">SIM Serial No.</th>
								<th style="width:15%">SAM Serial No.</th>
								<th style="width:15%">Installation Date</th>
							</tr>
						</thead><tbody>
						<%
							for(TreeMap<String, String> item : tid_facilities){
								html = "<tr>"
									+ "	<td class=\"mid_no\"><input type=\"text\" readonly=\"readonly\" value=\"" + item.get("mid_no") + "\" /></td>"
									+ "	<td class=\"mid_no2\"><input type=\"text\" readonly=\"readonly\" value=\"" + item.get("mid_no2") + "\" /></td>"
									+ "	<td class=\"tid_no\"><input type=\"text\" readonly=\"readonly\" value=\"" + item.get("tid_no") + "\" /></td>"
									+ "	<td class=\"tid_serial\"><input type=\"text\" value=\"" + item.get("tid_serial") + "\" ></td>"
									+ "	<td class=\"sim_serial\"><input type=\"text\" value=\"" + item.get("sim_serial") + "\" ></td>"
									+ "	<td class=\"sam_serial\"><input type=\"text\" value=\"" + item.get("sam_serial") + "\" ></td>"
									+ "	<td class=\"install_date\"><input type=\"text\" placeholder=\"ddmmyyyy\" value=\"" + item.get("install_date") + "\" ></td>"
									+ "</tr>";
								
								out.println(html);
							}
						%>
						</tbody></table>
						
					</div>
					
					<div class="form-section">
						<div class="title">Remarks</div>
						
						<table class="tblDetails" style="width:90%"><thead>
							<tr>
								<th style="width:50%">Note / Comment</th>
								<th style="width:15%">Status</th>
								<th style="width:15%">By</th>
								<th style="width:20%">Date</th>
							</tr>
						</thead><tbody>
						<%
						if(request.getAttribute("c_outlet") != null) {
							ArrayList<TreeMap<String, String>> histArr = ((CaseOutlet) request.getAttribute("c_outlet")).getInstallNotes();
							if(histArr != null){
								int n = 0;
								for(TreeMap<String, String> histLog : histArr){
									html = "<tr class=" + (n % 2 > 0 ? "alt" : "even") + ">"
									+ "	<td>" + Encode.forHtml(histLog.get("note")) + "</td>"
									+ "	<td>" + Encode.forHtml(histLog.get("status")) + "</td>"
									+ "	<td>" + Encode.forHtml(histLog.get("created_by")) + "</td>"
									+ "	<td>" + Casemast.formatCreatedDate("dd/MM/yyyy HH:mm", Long.parseLong(histLog.get("created_date"))) + "</td>"
									+ "</tr>";
									
									out.println(html);
									++n;
								}
							}
						}
						%>
						</tbody></table>
						
						<table style="width:100%">
							<tr>
								<td style="width:20%">Remark</td>
								<td style="width:80%"><textarea id="remark" name="remark" style="height:60px; width:450px"></textarea></td>
							</tr>
						</table>
					</div>
					
				</div>
			</div>
			
			<div class="btn-control">
				<input id="click_redirect" name="click_redirect" type="hidden" value="" />
				<%-- <button type="button" id="submit" class="btn" onclick="vendor.create_vendor_installation('<%= merchant_id %>', '<%= case_id %>');">Save</button> --%>
				<button type="button" id="submit" class="btn" onclick="vendor.create_vendor_installation('<%= str_merchant_id %>', '<%= str_case_id %>');">Save</button>
			</div>
		
		</div>
		</form>
		
	</div>
</div>

<!-- Footer -->
<%@ include file="/jsp/common/Footer.jsp" %>	
<!-- End Footer -->


</body>
</html>